const Koa = require('koa');
const serve = require('koa-static');
const bodyParser = require('koa-bodyparser');
const path = require('path');
const mount = require('koa-mount');

const Router = require('@koa/router');
const userRouter = require('./routes/user')
const sightRouter = require('./routes/sight')

const router = new Router();

const cors = require('@koa/cors');
console.log('123456')
console.log('zcp')
console.log('有点意思')

const app = new Koa();
app.use(bodyParser());

// app.use(async ctx => {
//     ctx.body = {
//         title:'hello koa',
//         result:ctx.request.body
//     }

  
// });

// Define routes
// router.get('/', async (ctx) => {
//     ctx.body = {
//         title: 'Hello Koa',
//         message: 'Welcome to the Koa Router example!'
//     };
// });

// router.post('/addUser',(ctx,next)=>{
//      ctx.body = {
//         title: 'Hello Koa',
//         result: ctx.request.body
//     };
// })

//托管静态资源文件夹
app.use(mount('/ss',serve(path.join(__dirname, 'static'))));

//koa跨域配置
app.use(cors({
    origin(ctx){
        return '*';
    }
}))

// Use router middleware
router.use(userRouter.routes())
router.use(sightRouter.routes())

app.use(router.routes());
app.use(router.allowedMethods());
app.use(cors());

app.listen(3000)
console.log('listening on port 3000');